<template>
  <div class="login-page">
    <a-form
      :model="formState"
      name="basic"
      autocomplete="off"
      class="form-wrapper"
      @finish="onFinish"
      @finishFailed="onFinishFailed"    
    >
      <div class="header">LOGIN</div>
      <a-form-item
        name="username"
        class="input-wrapper"
        :rules="[{ required: true, message: 'Please input your username!' }]"
      >
        <div class="border-wrapper">
          <a-input v-model:value="formState.username" placeholder="USERNAME" />
        </div>
      </a-form-item>

      <a-form-item
        name="password"
        class="input-wrapper"
        :rules="[{ required: true, message: 'Please input your password!' }]"
      >
        <div class="border-wrapper input-password">
          <a-input-password v-model:value="formState.password" placeholder="PASSWORD" />
        </div>
      </a-form-item>

      <a-form-item name="remember" :wrapper-col="{ offset: 8, span: 16 }">
        <a-checkbox v-model:checked="formState.remember">Remember me</a-checkbox>
      </a-form-item>

      <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
        <a-button class="btn" html-type="submit">Submit</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { getCurrentInstance, ref, reactive } from 'vue';
import { login } from '@/api/auth';
import { setToken } from '@/utils/token'

  const loginInfo = ref({
    username: 'admin',
    password: '123456'
  })

  const formState = reactive({
    username: 'admin',
    password: '123456',
    remember: false
  })

  const onFinish = async (values: any) => {
    console.log('Success:', values);
    const res = await login(values)
    res.data.token && setToken(res.data.token);
    globalProperties.$message.success('登录成功');
    globalProperties.$router.push('/')
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };

  // 当前挂载app的方法
  const globalProperties: any = getCurrentInstance()?.appContext.config.globalProperties

</script>
<style lang="scss">
.login-page {
  .ant-checkbox + span {
    color: #0eb4dd;
  }
}
</style>
<style lang="scss" scoped>
.login-page {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgb(121, 141, 233);
  .form-wrapper {
    width: 380px;
    background-color: rgb(41, 45, 62);
    color: #fff;
    border-radius: 2px;
    padding: 50px;
  }
  .form-wrapper .header {
    text-align: center;
    font-size: 35px;
    text-transform: uppercase;
    line-height: 100px;
  }
  .form-wrapper .btn {
    width: 60%;
    text-transform: uppercase;
    border: 2px solid #0e92b3;
    color: #40a9ff;
    background-color: rgb(41, 45, 62);
    text-align: center;
    border-radius: 30px;
    cursor: pointer;
  }
}
</style>
